package com.pfsbuilder.Dialogs;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JTabbedPane;

import com.pfsbuilder.PExceptionDialog;
import com.pfsbuilder.PFSPanels.DIBPanel;
import com.pfsbuilder.PFSPanels.DIRPanel;
import com.pfsbuilder.PFSPanels.FABPanel;
import com.pfsbuilder.PFSPanels.FIBPanel;
import com.pfsbuilder.PFSPanels.FILEContentPanel;
import com.pfsbuilder.PFSPanels.FILEPanel;
import com.pfsbuilder.PFSPanels.SBPanel;

/**
 * <p>
 * Title: PFS
 * </p>
 *
 * <p>
 * Description:
 * </p>
 *
 * <p>
 * Copyright: Copyright (c) 2005
 * </p>
 *
 * <p>
 * Company: Petersoft
 * </p>
 *
 * @author Peter
 * @version 2005
 */

public class BlockDialog extends JDialog {
	byte data[];
	BlockDialog_TableModel jBlockDialog_TableModel = new BlockDialog_TableModel();
	BorderLayout borderLayout7 = new BorderLayout();
	JTabbedPane JTabbedPane1 = new JTabbedPane();
	int blockNo;

	public BlockDialog() {
		super();
		try {
			jbInit();
		} catch (Exception ex) {
			ex.printStackTrace();
			if (com.pfsbuilder.Global.GUI) {
				new PExceptionDialog(ex, com.pfsbuilder.Global.sessionNumber).setVisible(true);
			}
		}
	}

	public BlockDialog(JFrame owner, String blockNo, byte data[]) {
		super(owner, "Block Number : " + blockNo, true);
		try {
			this.blockNo = Integer.parseInt(blockNo);
			initData(data);
		} catch (Exception ex) {
			ex.printStackTrace();
			if (com.pfsbuilder.Global.GUI) {
				new PExceptionDialog(ex, com.pfsbuilder.Global.sessionNumber).setVisible(true);
			}
		}
	}

	public BlockDialog(JDialog owner, String blockNo, byte data[]) {
		super(owner, "Block Number : " + blockNo, true);
		try {
			this.blockNo = Integer.parseInt(blockNo);
			initData(data);
		} catch (Exception ex) {
			ex.printStackTrace();
			if (com.pfsbuilder.Global.GUI) {
				new PExceptionDialog(ex, com.pfsbuilder.Global.sessionNumber).setVisible(true);
			}
		}
	}

	private void initData(byte data[]) {
		try {
			this.data = data;
			jbInit();
			if (data[0] == 'S' && data[1] == 'B') {
				JTabbedPane1.add(new BlockDialogPanel(this, new SBPanel(this, data), blockNo), "SB");
			} else if (data[0] == 'D' && data[1] == 'I' && data[2] == 'R') {
				JTabbedPane1.add(new BlockDialogPanel(this, new DIRPanel(this, data), blockNo), "Dir");
			} else if (data[0] == 'F' && data[1] == 'A' && data[2] == 'B') {
				JTabbedPane1.add(new BlockDialogPanel(this, new FABPanel(data), blockNo), "FAB");
			} else if (data[0] == 'D' && data[1] == 'I' && data[2] == 'B') {
				JTabbedPane1.add(new BlockDialogPanel(this, new DIBPanel(this, data), blockNo), "DIB");
			} else if (data[0] == 'F' && data[1] == 'I' && data[2] == 'L' && data[3] == 'E') {
				JTabbedPane1.add(new BlockDialogPanel(this, new FILEPanel(this, data), blockNo), "File");
			} else if (data[0] == 'F' && data[1] == 'I' && data[2] == 'B') {
				JTabbedPane1.add(new BlockDialogPanel(this, new FIBPanel(this, data), blockNo), "FIB");
			} else {
				JTabbedPane1.add(new BlockDialogPanel(this, new FILEContentPanel(data), blockNo), "Data");
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			if (com.pfsbuilder.Global.GUI) {
				new PExceptionDialog(ex, com.pfsbuilder.Global.sessionNumber).setVisible(true);
			}
		}
	}

	private void jbInit() throws Exception {
		jBlockDialog_TableModel.setValue(blockNo);

		setSize(600, 600);
		centerDialog();
		this.getContentPane().add(JTabbedPane1, java.awt.BorderLayout.CENTER);
	}

	public JTabbedPane getTabbedPane() {
		return JTabbedPane1;
	}

	void centerDialog() {
		// Center the window
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		Dimension frameSize = this.getSize();

		if (frameSize.height > screenSize.height) {
			frameSize.height = screenSize.height;
		}

		if (frameSize.width > screenSize.width) {
			frameSize.width = screenSize.width;
		}

		this.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);

	}
}
